package zhcms.lyb;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import zhcms.db.DBManager;
import zhcms.db.ConnectionPool.PooledConnection;
import zhcms.framework.Action;
import zhcms.framework.ActionForward;


public class GyLybAdd implements Action{

	@Override
	public ActionForward excute(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}

	public ActionForward add(HttpServletRequest request, HttpServletResponse response) {
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e1) {
		}
		String verifycode=request.getParameter("verifycode");
		String contents=request.getParameter("contents");
		String title=request.getParameter("title");
		System.out.println(title);
		//验证码判断
		String checkcode = (String)request.getSession().getAttribute("checkcode"); 
		if(verifycode == null || verifycode.length() < 1) 
		{
			return new ActionForward("/lyb/error.jsp?errorurl=/lyb/add.jsp&errorcause=验证码没有填写",2);
		}
		else
		{
			if(verifycode.equals(checkcode))
			{
				
			}
			else
			{
				return new ActionForward("/lyb/error.jsp?errorurl=/lyb/add.jsp&errorcause=验证码错误",2);
			}
		}
		
		if(contents == null || contents.length() < 1) 
		{
			return new ActionForward("/lyb/error.jsp?errorurl=/lyb/add.jsp&errorcause=内容没有填写",2);
		}
		
		if(title == null || title.length() < 1) 
		{
			return new ActionForward("/lyb/error.jsp?errorurl=/lyb/add.jsp&errorcause=主题没有填写",2);
		}
	
		PooledConnection conn = null;
		String sqlStr1="insert into lyb(title,content,addtime) values('"+ title +"','"+contents+"',now())";
		boolean isok=false;
		try {
			conn = DBManager.getConnection();
			conn.executeUpdate(sqlStr1);
			conn.close();
			isok=true;
		} catch (SQLException e) {
			isok=false;
			e.printStackTrace();
		}
		finally {
            conn.close();
		}
		
		if(isok==true)
		{
			return new ActionForward("/lyb/list.jsp",1);
		}
		else
		{
			return new ActionForward("/lyb/error.jsp?errorurl=/lyb/add.jsp&errorcause=主题没有填写",2);
		}
		
	}
}
